package com.yanqu.road.server.cmd.activity.pageant;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.math.BigInteger;

@Cmd(code = CrossProtocol.C2_PAGEANT_DETAIL, desc = "选美详情")
public class CrossPageantDetailCmd extends CrossCommand {
    private int activityId;
    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PageantProto.CrossPageantDetailReqMsg req = PageantProto.CrossPageantDetailReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityRankListModel singleRankModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, eGamePlayerEventType.PageantSingleScoreCrossRank.getValue());
        CrossActivityRankListModel totalRankModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, eGamePlayerEventType.PageantTotalScoreCrossRank.getValue());

        int singleRank = singleRankModel.getMyRank(userId, serverId);
        int totalRank = ActivityRankMgr.getNewCrossActivityRank(activityId, eGamePlayerEventType.PageantTotalScoreCrossRank.getValue(), serverId, userId);
        PageantProto.CrossPageantDetailRespMsg.Builder msg = PageantProto.CrossPageantDetailRespMsg.newBuilder();
        msg.setSingleRank(singleRank);
        msg.setTotalRank(totalRank);
        if (singleRank != 1 && singleRank != -1) {
            BigInteger nextValue = singleRankModel.getRankList().get(singleRank - 2).getValue();
            BigInteger myValue = singleRankModel.getRankList().get(singleRank - 1).getValue();
            msg.setNextSingleValue(nextValue.subtract(myValue).intValue());
        } else {
            msg.setNextSingleValue(-1);
        }
        if (totalRank != 1 && totalRank != -1) {
            BigInteger nextValue = totalRankModel.getRankList().get(totalRank - 2).getValue();
            BigInteger myValue = totalRankModel.getRankList().get(totalRank - 1).getValue();
            msg.setNextTotalValue(nextValue.subtract(myValue).intValue());
        } else {
            msg.setNextTotalValue(-1);
        }
        msg.setActivityId(activityId);
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_PAGEAN_DETAIL, msg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
